package fi.foyt.cs.persistence.dao.oauth;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import fi.foyt.cs.persistence.dao.GenericDAO;
import fi.foyt.cs.persistence.domainmodel.oauth.AuthorizedClient;

public class AuthorizedClientDAO extends GenericDAO<AuthorizedClient> {

  public AuthorizedClient create(String clientId, String clientSecret, String returnUrl, String clientName, Boolean autoAcceptScopes) {
    EntityManager entityManager = getEntityManager();

    entityManager.getTransaction().begin();
    try {
      AuthorizedClient authorizedClient = new AuthorizedClient();
      authorizedClient.setClientId(clientId);
      authorizedClient.setClientSecret(clientSecret);
      authorizedClient.setReturnUrl(returnUrl);
      authorizedClient.setClientName(clientName);
      authorizedClient.setAutoAcceptScopes(autoAcceptScopes);
      entityManager.persist(authorizedClient);
      entityManager.getTransaction().commit();
      return authorizedClient;
    } finally {
      if (entityManager.getTransaction().isActive())
        entityManager.getTransaction().rollback();
    }
  }
  
  public AuthorizedClient findByClientId(String clientId) {
    EntityManager entityManager = getEntityManager();
    Query query = entityManager.createQuery("select o from AuthorizedClient o where clientId = :clientId");
    query.setParameter("clientId", clientId);
    return getSingleResult(query);
  }
  
  public AuthorizedClient findByClientIdAndClientSecret(String clientId, String clientSecret) {
    EntityManager entityManager = getEntityManager();
    Query query = entityManager.createQuery("select o from AuthorizedClient o where clientId = :clientId and clientSecret = :clientSecret");
    query.setParameter("clientId", clientId);
    query.setParameter("clientSecret", clientSecret);
    return getSingleResult(query);
  }

  public AuthorizedClient updateReturnUrl(AuthorizedClient authorizedClient, String returnUrl) {
    EntityManager entityManager = getEntityManager();

    entityManager.getTransaction().begin();
    try {
      authorizedClient.setReturnUrl(returnUrl);
      authorizedClient = entityManager.merge(authorizedClient);
      entityManager.getTransaction().commit();
      return authorizedClient;
    } finally {
      if (entityManager.getTransaction().isActive())
        entityManager.getTransaction().rollback();
    }
  }

  public AuthorizedClient updateClientName(AuthorizedClient authorizedClient, String clientName) {
    EntityManager entityManager = getEntityManager();

    entityManager.getTransaction().begin();
    try {
      authorizedClient.setClientName(clientName);
      authorizedClient = entityManager.merge(authorizedClient);
      entityManager.getTransaction().commit();
      return authorizedClient;
    } finally {
      if (entityManager.getTransaction().isActive())
        entityManager.getTransaction().rollback();
    }
  }

  public AuthorizedClient updateAutoAcceptScopes(AuthorizedClient authorizedClient, Boolean autoAcceptScopes) {
    EntityManager entityManager = getEntityManager();

    entityManager.getTransaction().begin();
    try {
      authorizedClient.setAutoAcceptScopes(autoAcceptScopes);
      authorizedClient = entityManager.merge(authorizedClient);
      entityManager.getTransaction().commit();
      return authorizedClient;
    } finally {
      if (entityManager.getTransaction().isActive())
        entityManager.getTransaction().rollback();
    }
  } 
}
